package com.bone.modules.quartz.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bone.common.util.BoneUtils;
import com.bone.common.validation.group.AddDto;
import com.bone.common.validation.group.UpdateDto;
import com.bone.modules.quartz.dto.QuartzAddOrUpdateDto;
import com.bone.modules.quartz.dto.QuartzShowDto;
import io.swagger.annotations.ApiImplicitParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.bone.modules.quartz.service.IQuartzJobService;
import com.bone.modules.quartz.entity.QuartzJob;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bone.common.vo.Result;

import java.util.Arrays;
import java.util.List;

import org.springframework.web.bind.annotation.RestController;

/**
 * 定时任务表 前端控制器
 *
 * @author : 王可可
 * @date : 2020-12-10
 */
@Api(tags = {"定时任务"})
@RestController
@RequestMapping("/quartz/quartzJob")
public class QuartzJobController {
    @Autowired
    private IQuartzJobService quartzJobService;

    /**
     * 查询分页数据
     */
    @ApiOperation(value = "查询分页数据")
    @GetMapping(value = "/page")
    public Result<IPage<QuartzShowDto>> findListByPage(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        return Result.ok(BoneUtils.entityPageToModelPage(quartzJobService.page(new Page<>(pageNo, pageSize)), QuartzShowDto.class));
    }


    @ApiOperation("所有定时任务")
    @GetMapping("/list")
    public Result<List<QuartzShowDto>> queryAllScheduleTask() {
        return Result.ok(quartzJobService.queryList());
    }

    @ApiOperation("启动定时任务")
    @ApiImplicitParam(name = "id", value = "定时任务ID", required = true)
    @PutMapping("/start/{id}")
    public Result<String> startScheduledTask(@PathVariable("id") String id) {
        quartzJobService.resumeJob(id);
        return Result.ok();
    }

    @ApiOperation("立即执行定时任务")
    @ApiImplicitParam(name = "id", value = "定时任务ID", required = true)
    @PutMapping("/execute/{id}")
    public Result<String> executeScheduledTask(@PathVariable("id") String id) {
        try {
            quartzJobService.execute(quartzJobService.getById(id));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.ok();
    }

    @ApiOperation("暂停定时任务")
    @ApiImplicitParam(name = "id", value = "定时任务ID", required = true)
    @PutMapping("/stop/{id}")
    public Result<String> stopScheduledTask(@PathVariable("id") String id) {
        quartzJobService.pause(id);
        return Result.ok();
    }

    @ApiOperation("添加定时任务")
    @PostMapping("/add")
    public Result<String> addScheduledTask(@RequestBody @Validated(AddDto.class) QuartzAddOrUpdateDto quartzAddOrUpdateDto) {
        return quartzJobService.addOrUpdateTask(quartzAddOrUpdateDto) ? Result.ok("添加成功") : Result.error("添加失败");
    }

    @ApiOperation("修改定时任务")
    @PostMapping("/update")
    public Result<String> updateScheduledTask(@RequestBody @Validated(UpdateDto.class) QuartzAddOrUpdateDto quartzAddOrUpdateDto) {
        return quartzJobService.addOrUpdateTask(quartzAddOrUpdateDto) ? Result.ok("修改成功") : Result.error("修改失败");
    }

    /**
     * 根据id查询
     */
    @ApiOperation(value = "根据id查询数据")
    @GetMapping(value = "/getById")
    public Result<QuartzJob> getById(@RequestParam("id") String id) {
        return Result.ok(quartzJobService.getById(id));
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除数据")
    @DeleteMapping(value = "/del")
    public Result<String> delete(@RequestParam("ids") String ids) {
        return quartzJobService.removeByIds(Arrays.asList(ids.split(","))) ? Result.ok() : Result.error("删除失败");
    }


}
